package database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author baena and kadoura
 */
public class BusinessDelegate {

    public static void insertProfile(Profile profile) {
        try {
            DataService.getInstance().insertProfile(profile);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void deleteProfile(Profile profile) {
        try {
            DataService.getInstance().deleteProfile(profile);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Profile getProfile(String login) {
        return DataService.getInstance().getProfile(login);
    }

    public static void setStatus(Profile profile, Profile.Status status) {
        try {
            DataService.getInstance().setStatus(profile, status);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void setEmail(Profile profile, String email) {
        try {
            DataService.getInstance().setEmail(profile, email);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void setPassword(Profile profile, String password) {
        try {
            DataService.getInstance().setPassword(profile, password);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void insertMessage(Message message) {
        try {
            DataService.getInstance().insertMessage(message);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Insère une relation pas encore acceptée par le friend2
     *
     * @param friend1 : profile qui émet la demande de contact
     * @param friend2 : personne qui doit répondre à la demande
     */
    public static void insertPendingFriendship(Profile friend1, Profile friend2) {
        try {
            DataService.getInstance().insertPendingFriendship(friend1, friend2);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Valide la relation entre 2 contacts
     * (demande de contact acceptée par le friend 2)
     * 
     * @param friend1
     * @param friend2
     */
    public static void acceptFriendship(Profile friend1, Profile friend2) {
        DataService.getInstance().acceptFriendship(friend1, friend2);
    }

    // quand on refuse une demande de contact :
    public static void deleteFriendship(Profile friend1, Profile friend2) {
        try {
            DataService.getInstance().deleteFriendship(friend1, friend2);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Pour récupérer la liste de demande de contacts
     * 
     * @param profile
     * @return
     */
    public static ArrayList<String> getPendingFriendship(Profile profile){
        return DataService.getInstance().getPendingFriendship(profile);
    }
    
    // renvoie le nouveau nom du fichier
    public static String insertFile(File file) {
        String res = null;
        try {
            res = DataService.getInstance().insertFile(file);
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public static void dropAllTables() {
        DataService.getInstance().dropAllTables();
    }

    public static ArrayList<String> getContactList(Profile profile) {
        return DataService.getInstance().getContactList(profile);
    }

    /**
     * Retourne l'historique entier d'une conversation
     *
     * @param profile1
     * @param profile2
     * @return
     */
    public static ArrayList<Message> getAllConversation(Profile profile1, Profile profile2) {
        return DataService.getInstance().getAllConversation(profile1, profile2);
    }

    /**
     * Retourne les messages non lus (et les passe à lus)
     *
     * @param profile1
     * @param profile2
     * @return
     */
    public static ArrayList<Message> getUndeliveredConversation(Profile profile1, Profile profile2) {
        ArrayList<Message> res = DataService.getInstance().getUndeliveredConversation(profile1, profile2);
        DataService.getInstance().setViewedConversation(profile1, profile2);
        return res;
    }

    /**
     * Marque comme lus les messages non lus
     *
     * @param profile1
     * @param profile2
     */
    public static void setViewedMessages(Profile profile1, Profile profile2) {
        DataService.getInstance().setViewedConversation(profile1, profile2);
    }
    
    public static ArrayList<File> getAllFiles(Profile profile){
        return DataService.getInstance().getAllFiles(profile);
    }
    
    public static ArrayList<File> getFilesNotDownloaded(Profile profile){
        return DataService.getInstance().getNotDownloadedFiles(profile);
    }
    
    public static void setFileDownloaded(File file){
        DataService.getInstance().setFileDownloaded(file);
    }
    
    public static void setAllFilesDownloaded(Profile profile){
        DataService.getInstance().setAllFilesDownloaded(profile);
    }

    public static boolean isAdmin(Profile profile) {
        return DataService.getInstance().isAdmin(profile);
    }

    /**
     * Change profile to admin if is true or to a normal user if is false
     *
     * @param profile
     * @param isAdmin
     */
    public static void setAdmin(Profile profile, boolean isAdmin) {
        DataService.getInstance().setAdmin(profile, isAdmin);
    }

    public static boolean correctPassword(String login, String password) {
        return DataService.getInstance().correctPassword(login, password);
    }

    public static ArrayList<String> searchContactByLogin(String search) {
        return DataService.getInstance().searchFriendByLogin(search);
    }

    public static ArrayList<String> searchContactByEmail(String search) {
        return DataService.getInstance().searchFriendByEmail(search);
    }

    public static ArrayList<String> searchContactByLastName(String search) {
        return DataService.getInstance().searchFriendByLastName(search);
    }
    
    public static void incrementNbConnections(Profile profile){
        DataService.getInstance().incrementNbConnections(profile);
    }
    
    public static int getNbProfiles(){
        return DataService.getInstance().getNbProfiles();
    }
    
    public static int getNbMessages(){
        return DataService.getInstance().getNbMessages();
    }
    
    public static int getNbFiles(){
        return DataService.getInstance().getNbFiles();
    }
    
    public static int getNbConnections(){
        return DataService.getInstance().getNbConnections();
    }
}
